package parser

import (
	"encoding/json"
	"mi/compiler/ast"
	"mi/internal/xpath"
	"path/filepath"
)

func ParseProjectFile(uri xpath.URI, sourceText []byte) *ast.ProjectFile {
	var project = new(ast.ProjectFile)
	project.SetPos(0)
	project.SetEnd(len(sourceText))
	project.Uri = uri
	project.Text = sourceText
	project.Packages = make(map[string]*ast.Package)
	project.ResolvedPackageTree = make(map[*ast.PackageTree]*ast.Package)

	var err = json.Unmarshal(sourceText, project)
	if err != nil {
		panic(err)
	}

	var base = filepath.Dir(uri.Filename())
	project.SourcePath = filepath.Join(base, "源码")
	project.OutputPath = filepath.Join(base, "输出")
	return project
}